<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <!-- <el-form-item label="技师id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入技师id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="门店名称" prop="belongStoreName">
        <el-input
          v-model="queryParams.belongStoreName"
          placeholder="请输入门店名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="工号" prop="jobNumber">
        <el-input
          v-model="queryParams.jobNumber"
          placeholder="请输入工号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名字" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="时间" prop="searchTime">
        <el-date-picker
          @change="getTime"
          v-model="searchTime"
          type="daterange"
          align="right"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="手机号" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归属门店id" prop="belongStoreId">
        <el-input
          v-model="queryParams.belongStoreId"
          placeholder="请输入归属门店id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归属区域名称" prop="belongStoreName">
        <el-input
          v-model="queryParams.belongStoreName"
          placeholder="请输入归属区域名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归属区域id" prop="belongAreaId">
        <el-input
          v-model="queryParams.belongAreaId"
          placeholder="请输入归属区域id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="belong_area_name" prop="belongAreaName">
        <el-input
          v-model="queryParams.belongAreaName"
          placeholder="请输入belong_area_name"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="月份" prop="monthTime">
        <el-input
          v-model="queryParams.monthTime"
          placeholder="请输入月份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="底薪" prop="basicSalary">
        <el-input
          v-model="queryParams.basicSalary"
          placeholder="请输入底薪"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目收益" prop="itemEarnings">
        <el-input
          v-model="queryParams.itemEarnings"
          placeholder="请输入项目收益"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="附加商品收益" prop="productEarnings">
        <el-input
          v-model="queryParams.productEarnings"
          placeholder="请输入附加商品收益"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="包间收益" prop="compartmentEarnings">
        <el-input
          v-model="queryParams.compartmentEarnings"
          placeholder="请输入包间收益"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员卡收益" prop="memberEarnings">
        <el-input
          v-model="queryParams.memberEarnings"
          placeholder="请输入会员卡收益"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="储值卡收益" prop="valueEarnings">
        <el-input
          v-model="queryParams.valueEarnings"
          placeholder="请输入储值卡收益"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生日奖励" prop="birthdayReward">
        <el-input
          v-model="queryParams.birthdayReward"
          placeholder="请输入生日奖励"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="社保扣除" prop="socialSecurity">
        <el-input
          v-model="queryParams.socialSecurity"
          placeholder="请输入社保扣除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          v-hasPermi="['business:masterSalary:query']"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:wage:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:wage:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:wage:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:wage:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="wageList"
      max-height="650px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="" align="center" prop="id" /> -->
      <!-- <el-table-column label="技师id" align="center" prop="userId" /> -->
      <!-- <el-table-column label="月份" align="center" prop="monthTime" /> -->

      

      <el-table-column
        label="所属区域"
        align="center"
        :show-overflow-tooltip="true"
        prop="belongAreaName"
        width="150px"
      />

      <el-table-column
        label="所属门店"
        align="center"
        :show-overflow-tooltip="true"
        prop="belongStoreName"
        width="150px"
      />

      <el-table-column
        label="技师名称"
        align="center"
        :show-overflow-tooltip="true"
        prop="name"
      />
      <el-table-column label="工号" align="center" prop="jobNumber" />
   
      <el-table-column
        label="手机号"
        align="center"
        width="110px"
        prop="phoneNumber"
      />
      <!-- <el-table-column label="归属门店id" align="center" prop="belongStoreId" /> -->
      
      <!-- <el-table-column label="归属区域id" align="center" prop="belongAreaId" /> -->
      <!-- <el-table-column label="belong_area_name" align="center" prop="belongAreaName" /> -->
      <!-- <el-table-column label="月份" align="center" prop="monthTime" /> -->
      <el-table-column label="底薪" align="center" prop="basicSalary" />
      <el-table-column label="项目收益" align="center" prop="itemEarnings" />
      <el-table-column
        label="附加商品收益"
        align="center"
        prop="productEarnings"
        width="100px"
      />
      <el-table-column
        label="包间收益"
        align="center"
        prop="compartmentEarnings"
      />
      <el-table-column
        label="会员卡收益"
        align="center"
        prop="memberEarnings"
      />
      <el-table-column label="储值卡收益" align="center" prop="valueEarnings" />
      <el-table-column label="退单收益" align="center" prop="refundEarnings" />
      
       <el-table-column label="奖励" align="center" prop="birthdayReward" /> 
       <el-table-column label="社保扣除" align="center" prop="socialSecurity" />
      <el-table-column label="其他扣除" align="center" prop="deductMoney" /> 
      <el-table-column label="总计" align="center">
        <template slot-scope="scope">
          {{
            (Number(scope.row.totalEarnings) -
            Number(scope.row.socialSecurity) -
            Number(scope.row.deductMoney)).toFixed(2)
          }}
        </template>
      </el-table-column>

      <!-- <el-table-column label="创建时间"   align="center"  prop="createTime" />
      <el-table-column label="修改时间"   align="center"  prop="updateTime" />
      <el-table-column label="修改人"   align="center"  prop="userId" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="150px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:masterSalary:edit']"
            >修改</el-button
          >
           <el-button
            size="mini"
            type="text"
            @click="handleDetail(scope.row)"
            v-hasPermi="['business:masterSalary:detail']"
            >详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:wage:remove']"
          >删除</el-button>  
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改技师工资对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="800px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        v-if="isUpdate"
      >
        <!-- <el-form-item label="技师id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入技师id" />
        </el-form-item> -->
        <!-- <el-form-item label="工号" prop="jobNumber">
          <el-input v-model="form.jobNumber" placeholder="请输入工号" />
        </el-form-item>
        <el-form-item label="名字" prop="name">
          <el-input v-model="form.name" placeholder="请输入名字" />
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="归属门店id" prop="belongStoreId">
          <el-input v-model="form.belongStoreId" placeholder="请输入归属门店id" />
        </el-form-item>
        <el-form-item label="归属区域名称" prop="belongStoreName">
          <el-input v-model="form.belongStoreName" placeholder="请输入归属区域名称" />
        </el-form-item>
        <el-form-item label="归属区域id" prop="belongAreaId">
          <el-input v-model="form.belongAreaId" placeholder="请输入归属区域id" />
        </el-form-item>
        <el-form-item label="belong_area_name" prop="belongAreaName">
          <el-input v-model="form.belongAreaName" placeholder="请输入belong_area_name" />
        </el-form-item>
        <el-form-item label="月份" prop="monthTime">
          <el-input v-model="form.monthTime" placeholder="请输入月份" />
        </el-form-item> -->

        <el-row>
          <!-- <el-col :span="12">
            <el-form-item label="月份" prop="monthTime" label-width="100px">
              <el-date-picker
                v-model="form.monthTime"
                type="month"
                placeholder="选择月"
                value-format="yyyy-MM"
              >
              </el-date-picker>
            </el-form-item>
          </el-col> -->

          <el-col :span="12">
            <el-form-item label="底薪" prop="basicSalary" label-width="100px">
              <el-input
                v-model="form.basicSalary"
                placeholder="请输入底薪"
                style="width: 222px"
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item
              label="奖励"
              prop="birthdayReward"
              label-width="100px"
            >
              <el-input
                v-model="form.birthdayReward"
                placeholder="请输入奖励"
                style="width: 222px"
              />
            </el-form-item>
          </el-col>
        </el-row>



        <!-- <el-row>
          <el-col :span="12">
            <el-form-item label="项目收益" prop="itemEarnings" label-width="100px">
              <el-input v-model="form.itemEarnings" placeholder="请输入项目收益" style="width: 222px" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="附加商品收益" label-width="100px" prop="productEarnings">
              <el-input v-model="form.productEarnings" placeholder="请输入附加商品收益" style="width: 222px" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="包间收益" prop="compartmentEarnings" label-width="100px">
              <el-input v-model="form.compartmentEarnings" placeholder="请输入包间收益" style="width: 222px" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会员卡收益" label-width="100px" prop="memberEarnings">
              <el-input v-model="form.memberEarnings" placeholder="请输入会员卡收益" style="width: 222px" />
            </el-form-item>
          </el-col>
        </el-row> -->

        <el-row>
          <el-col :span="12">
            <el-form-item
              label="社保扣除"
              prop="socialSecurity"
              label-width="100px"
            >
              <el-input
                v-model="form.socialSecurity"
                placeholder="请输入社保扣除"
                style="width: 222px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="其他扣除"
              prop="deductMoney"
              label-width="100px"
            >
              <el-input
                v-model="form.deductMoney"
                placeholder="请输入其他扣除"
                style="width: 222px"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <!-- <el-col :span="12">
            <el-form-item label="储值卡收益" prop="valueEarnings" label-width="100px">
              <el-input v-model="form.valueEarnings" placeholder="请输入储值卡收益" style="width: 222px" />
            </el-form-item>
          </el-col> -->
     
        </el-row>
      </el-form>
      <el-descriptions title="" column="2" v-else>
        <el-descriptions-item label="工号">{{
          form.jobNumber
        }}</el-descriptions-item>
        <el-descriptions-item label="姓名">{{
          form.name
        }}</el-descriptions-item>
        <el-descriptions-item label="手机号">{{
          form.phoneNumber
        }}</el-descriptions-item>
        <el-descriptions-item label="归属门店">{{
          form.belongStoreName
        }}</el-descriptions-item>
        <el-descriptions-item label="归属区域">{{
          form.belongAreaName
        }}</el-descriptions-item>
        <el-descriptions-item label="底薪">{{
          form.basicSalary
        }}</el-descriptions-item>
        <el-descriptions-item label="项目收益">{{
          form.itemEarnings
        }}</el-descriptions-item>
        <el-descriptions-item label="附加商品收益">{{
          form.productEarnings
        }}</el-descriptions-item>
        <el-descriptions-item label="包间收益">{{
          form.compartmentEarnings
        }}</el-descriptions-item>
        <el-descriptions-item label="会员卡收益">{{
          form.memberEarnings
        }}</el-descriptions-item>
        <el-descriptions-item label="储值卡收益">{{
          form.valueEarnings
        }}</el-descriptions-item>
        <el-descriptions-item label="生日奖励">{{
          form.birthdayReward
        }}</el-descriptions-item>
        <el-descriptions-item label="社保扣除">{{
          form.socialSecurity
        }}</el-descriptions-item>
        <el-descriptions-item label="其他扣除">{{
          form.deductMoney
        }}</el-descriptions-item>
        <!-- <el-descriptions-item label="备注">
      <el-tag size="small">学校</el-tag>
    </el-descriptions-item>
    <el-descriptions-item label="联系地址">江苏省苏州市吴中区吴中大道 1188 号</el-descriptions-item> -->
      </el-descriptions>
      <div slot="footer" class="dialog-footer" v-if="isUpdate">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWage,
  getWage,
  delWage,
  addWage,
  updateWage,
} from "@/api/system/wage";

export default {
  name: "Wage",
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "昨天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "当月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              const now = new Date();
              // 设置当前月份的第一天（00:00:00）
              now.setDate(1);
              now.setHours(0, 0, 0, 0);
              start.setTime(
                start.getTime() - (start.getTime() - now.getTime())
              );
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "上月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              // 获取当前日期
              const now = new Date();
              // 获取上个月的第一天
              const firstDayOfLastMonth = new Date(
                now.getFullYear(),
                now.getMonth() - 1,
                1
              );
              // 获取上个月的最后一天
              const lastDayOfLastMonth = new Date(
                now.getFullYear(),
                now.getMonth(),
                0
              );
              start.setTime(firstDayOfLastMonth.getTime());
              end.setTime(lastDayOfLastMonth.getTime());
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      isUpdate: true,
      //技师id
      technId: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 技师工资表格数据
      wageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        jobNumber: null,
        name: null,
        phoneNumber: null,
        belongStoreId: null,
        belongStoreName: null,
        belongAreaId: null,
        belongAreaName: null,
        monthTime: null,
        basicSalary: null,
        itemEarnings: null,
        productEarnings: null,
        compartmentEarnings: null,
        memberEarnings: null,
        valueEarnings: null,
        startTime: "",
        endTime: "",
        birthdayReward: null,
        socialSecurity: null,
      },
      searchTime: [],

      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询技师工资列表 */
    getTime() {
      this.queryParams.startTime = this.searchTime[0];
      this.queryParams.endTime = this.searchTime[1];
    },

    getList() {
      this.loading = true;
      listWage(this.queryParams).then((response) => {
        this.wageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        jobNumber: null,
        name: null,
        phoneNumber: null,
        belongStoreId: null,
        belongStoreName: null,
        belongAreaId: null,
        belongAreaName: null,
        monthTime: null,
        basicSalary: null,
        itemEarnings: null,
        productEarnings: null,
        compartmentEarnings: null,
        memberEarnings: null,
        valueEarnings: null,
        birthdayReward: null,
        socialSecurity: null,
        createTime: null,
        startTime: "",
        endTime: "",
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.startTime = "";
      this.queryParams.endTime = "";
      this.searchTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加技师工资";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.isUpdate = true;
      this.reset();
      this.technId=row.technId;
      const id = row.id || this.ids;
     // getWage(id).then((response) => {
        this.form = row;
        this.open = true;
        this.title = "修改技师工资";
      //});
    },
    handleDetail(row) {
      this.isUpdate = false;
      this.reset();
      const id = row.id || this.ids;
      getWage(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "技师工资";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
         // if (this.form.technId != null) {
            updateWage(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          // } else {
          //   addWage(this.form).then((response) => {
          //     this.$modal.msgSuccess("新增成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除技师工资编号为"' + ids + '"的数据项？')
        .then(function () {
          return delWage(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/wage/export",
        {
          ...this.queryParams,
        },
        `wage_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style>
.dialog-footer {
  text-align: center;
}
</style>
